/*
 * reuse_object.h
 *
 *  Created on: Sep 15, 2010
 *      Author: thienlong
 */

#ifndef REUSE_OBJECT_H_
#define REUSE_OBJECT_H_

#include "stringbuffer.h"
#include "list.h"
#define DO_NOTHING NULL
typedef enum {
	STRING_BUFFER_TYPE,
	STRING_BUFFER_ARRAY_TYPE,
} data_type;

#define VOID_TYPE -1

typedef void (*free_data_cb)(void *data);
typedef void (*reset_data_cb)(void *data);

typedef struct {
	vector *items;
	free_data_cb free_cb;
	reset_data_cb reset_cb;
} array_pointer;

typedef struct {
	array_pointer *ptr;
	int typenum;
} array_pool;

extern array_pool* apool_new(int typenum);

extern void apool_init(array_pool *pool, int typenum);

extern void apool_setcb(array_pool *pool, data_type type, free_data_cb free_fn, reset_data_cb reset_fn);

extern void* apool_get(array_pool *pool, data_type type);

extern void apool_put(array_pool *pool, data_type type,
		void *data);

extern int apool_element_size(array_pool *pool, data_type type);

extern int apool_size(array_pool *pool);

extern void apool_free(array_pool *pool);

extern void apool_to_string(array_pool *pool, stringbuffer *buf);

#define TYPE_NUM 7

struct next_data {
	struct next_data *next;
};

typedef struct {
	struct next_data *next;
	free_data_cb free_cb;
	reset_data_cb reset_cb;
} linked_pointer;

typedef struct {
	linked_pointer *ptr;
	int typenum;
} linked_pool;


extern linked_pool* lpool_new(int typenum);

extern void lpool_init(linked_pool *cache, int typenum);

extern void* lpool_get_element(linked_pool *cache, data_type type);

extern void lpool_put_element(linked_pool *cache, data_type type,
		struct next_data *data);

extern int lpool_element_size(linked_pool *cache, data_type type);

extern int lpool_size(linked_pool *cache);

extern void lpool_free(linked_pool *cache);

extern void lpool_to_string(linked_pool *cache, stringbuffer *buf);
#endif
